package com.brilliance.project.common.exception;

import java.sql.SQLException;

/**
 * 自定义的异常，可以方便异常的分类
 * 
 * @author HLJ
 * 
 */
public class BrillianceException extends Exception {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * DB操作异常
	 */
	public static final String EX_DB_OPERATION = "EX_DB_OPERATION";
	/**
	 * DB连接异常
	 */
	public static final String EX_DB_CONNECTION = "EX_DB_CONNECTION";
	/**
	 * 程序自定义异常
	 */
	public static final String EX_PROGRAM = "EX_PROGRAM";
	/**
	 * 传入的异常
	 */
	private Throwable rootCause = null;
	/**
	 * 异常类型
	 */
	private String brilliance_exception_type = "";
	/**
	 * 如果是DB异常，把異异常状态和信息赋给这两个数
	 */
	private int errorCode = 0;
	private String sqlState = null;
	/**
	 * 其它的异常信息
	 */
	private String exMessage = "";

	/**
	 * description:构造函数，吧其他异常封装成BrillianceException
	 * 
	 * @param ex_type
	 *            異常類型
	 * @param rootCause
	 *            異常
	 */
	public BrillianceException(String ex_type, Throwable rootCause) {
		super(rootCause);
		this.rootCause = rootCause;
		this.brilliance_exception_type = ex_type;
	}

	/**
	 * 构造函数，吧其他异常封装成BrillianceException
	 * 
	 * @param ex_type
	 * @param exMessage
	 *            需要说明的自定义异常信息
	 * @param rootCause
	 */
	public BrillianceException(String ex_type, String exMessage,
			Throwable rootCause) {
		super(rootCause);
		this.rootCause = rootCause;
		this.brilliance_exception_type = ex_type;
		this.exMessage = exMessage;
		if (BrillianceException.EX_DB_OPERATION.equals(ex_type)
				&& rootCause instanceof SQLException) {
			SQLException sqle = (SQLException) rootCause;
			this.errorCode = sqle.getErrorCode();
			this.sqlState = sqle.getSQLState();
		}

	}

	public Throwable getRootCause() {
		return rootCause;
	}

	public void setRootCause(Throwable rootCause) {
		this.rootCause = rootCause;

	}

	public String getBrilliance_exception_type() {
		return brilliance_exception_type;
	}

	public void setBrilliance_exception_type(String brilliance_exception_type) {
		this.brilliance_exception_type = brilliance_exception_type;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public String getSqlState() {
		return sqlState;
	}

	public void setSqlState(String sqlState) {
		this.sqlState = sqlState;
	}

	public String getExMessage() {
		return this.getMessage() + " " + exMessage;
	}

	public void setExMessage(String exMessage) {
		this.exMessage = exMessage;
	}

}
